package com.dingwei.lesyndrome.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.CoachDetailsAdapter;
import com.dingwei.lesyndrome.bean.CarTimeInfoBean;
import com.dingwei.lesyndrome.mvp.presenter.CoachDetailsPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.utils.IntentUtils;
import com.lihao.baselibrary.utils.RegexUtils;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/28.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CoachDetailsActivity extends BaseActivity implements Contract.CoachDetailsView {

    CoachDetailsPresenter coachDetailsPresenter = new CoachDetailsPresenter(this);


    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private String id;
    private CoachDetailsAdapter adapter;
    private CarTimeInfoBean bean;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_coach_details;
    }

    @Override
    protected void initData() {
        coachDetailsPresenter.initData(id);
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");

        adapter = new CoachDetailsAdapter(BaseActivity.context);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.bg1), DisplayUtil.dip2px(BaseActivity.context, 8)));
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void setData(CarTimeInfoBean bean) {
        this.bean = bean;
        titleBar.setTitleText(bean.data.info.title);
        tvPhone.setText("时刻表仅供参考，实际发车时间请联系客服 " + bean.data.info.cservice);

        adapter.addAll(bean.data.list);

        showData("success");
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            if (adapter.getAllData()!=null&&adapter.getAllData().size()>0){
                dataLayout.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            }else {
                dataLayout.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            noNetLayout.setVisibility(View.VISIBLE);
            dataLayout.setVisibility(View.GONE);
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.tv_phone, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_phone:
                intent = IntentUtils.getDialIntent(bean.data.info.cservice);
                startActivity(intent);
                break;
            case R.id.no_net_btn:
                coachDetailsPresenter.initData(id);
                break;
        }
    }
}
